package com.hup.minicontroller.client.view.connected;

import com.hup.minicontroller.common.model.dto.FullScreenTouchRequest;
import com.hup.utils.swing.customView.ImageLabel;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author hugan
 * @date 2021/5/25
 */
@Log4j2
public class ScreenView extends ImageLabel {

    @Setter
    private TouchEventCallback touchEventCallback;

    public ScreenView() {
        MyTouchListener myTouchListener = new MyTouchListener();
        addMouseListener(myTouchListener);
        addMouseMotionListener(myTouchListener);
    }

    private class MyTouchListener extends MouseAdapter {
        private int lastX, lastY;
        /**
         * 是否为要处理的触摸事件;
         * 按下时,不在图像区域,一律过滤
         */
        private boolean touching = false;

        @Override
        public void mousePressed(MouseEvent e) {
            onTouch(e, FullScreenTouchRequest.ACTION_DOWN);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            onTouch(e, FullScreenTouchRequest.ACTION_MOVE);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            onTouch(e, FullScreenTouchRequest.ACTION_UP);
        }

        private void onTouch(MouseEvent e, int action) {
            if (touchEventCallback == null) return;
            int x = e.getX();
            int y = e.getY();
            switch (action) {
                case FullScreenTouchRequest.ACTION_DOWN:
                    touching = getIconRect().contains(x, y);
                    if (!touching) return;
                    //log.info("ACTION_DOWN");
                    break;
                case FullScreenTouchRequest.ACTION_MOVE:
                    if (!touching) return;
                    if (Math.abs(lastX - x) < 2 && Math.abs(lastY - y) < 2) return;//过滤微小移动
                    //log.info("ACTION_MOVE");
                    break;
                case FullScreenTouchRequest.ACTION_UP:
                    if (!touching) return;
                    //log.info("ACTION_UP");
                    touching = false;
                    break;
            }
            lastX = x;
            lastY = y;
            handleEvent(action, x, y);
        }

        private void handleEvent(int action, int x, int y) {
            float xPercent = (x - getIconRect().x + 0f) / getIconRect().width;
            if (xPercent < 0) xPercent = 0;
            else if (xPercent > 1) xPercent = 1;
            float yPercent = (y - getIconRect().y + 0f) / getIconRect().height;
            if (yPercent < 0) yPercent = 0;
            else if (yPercent > 1) yPercent = 1;
            touchEventCallback.handleEvent(action, xPercent, yPercent);
        }

    }

    /**
     * 触摸事件回调
     */
    public interface TouchEventCallback {
        /**
         * @param action {@link FullScreenTouchRequest}.action
         * @param xPercent 坐标位置百分比
         * @param yPercent 坐标位置百分比
         */
        void handleEvent(int action, float xPercent, float yPercent);
    }
}
